<?php   

class LoginController extends Zend_Controller_Action
{

    public function init()
    {
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
    }

    public function indexAction()
    {
        
          //esta accion no usara layout.phtml
        echo '<link rel="stylesheet" href="css/login.css" type="text/css" />';
        $this->_helper->layout->disableLayout();
        //esta accion no renderizara su contenido en saludoajax2.phtml
        //$this->_helper->viewRenderer->setNoRender();
        $this->view->title = "Log In";
            //valor para <head><title>
//            $this->view->headTitle($this->view->title);
        
        $login = new Application_Form_Login();
		$this->view->form = $login;
		
		if ($this->_request->isPost())
		{
			$zend_auth = Zend_Auth::getInstance();
			
			$dbAdapter = Zend_Db_Table_Abstract::getDefaultAdapter();
	 
			$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
	 
			$authAdapter
				->setTableName('usuario')
				->setIdentityColumn('nick')
				->setCredentialColumn('pass')
				->setIdentity($this->getRequest()->getPost('nick'))
				->setCredential($this->getRequest()->getPost('pass'));
					
			$result = Zend_Auth::getInstance()->authenticate($authAdapter);	
			
			if ($result->isValid()) {
				
				$storage = Zend_Auth::getInstance()->getStorage();
				$save_result = $authAdapter->getResultRowObject(null,'pass');
				$storage->write($save_result);

				$this->_redirect('index');
		
	
			
			}  else
                        {
                            $this->_redirect('login');
                        }
			
			
		}
    }

    public function ingresarAction()
    {
        $login = new Application_Form_Login();
		$this->view->form = $login;
		
		if ($this->_request->isPost())
		{
			$zend_auth = Zend_Auth::getInstance();
			
			$dbAdapter = Zend_Db_Table_Abstract::getDefaultAdapter();
	 
			$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
	 
			$authAdapter
				->setTableName('usuario')
				->setIdentityColumn('nick')
				->setCredentialColumn('pass')
				->setIdentity($this->getRequest()->getPost('nick'))
				->setCredential($this->getRequest()->getPost('pass'));
					
			$result = Zend_Auth::getInstance()->authenticate($authAdapter);	
			
			if ($result->isValid()) {
				
				$storage = Zend_Auth::getInstance()->getStorage();
				$save_result = $authAdapter->getResultRowObject(null,'pass');
				$storage->write($save_result);

				$this->_redirect('cliente');
		
	
			
			}  else
                        {
                            $this->_redirect('login');
                        }
			
			
		}
    
    }

    public function salirAction()
    {
         Zend_Auth::getInstance()->clearIdentity();
         $this->_helper->redirector('index'); 
    }


}





